/*
 * JasperRerportXML.java
 *
 * Created on 30 de agosto de 2007, 01:06 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ig.formato.util;

import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author jsanchez
 */
public class JasperRerportXML {

    public static Integer B_TITULO = 1;
    public static Integer B_ENCABEZADO = 2;
    public static Integer B_DETALLE = 3;
    public static String TOMAR_DATO_DE_FIELD = "F";
    public static String TOMAR_DATO_DE_PARAMETRO = "P";
    public static String Center = "Center";
    public static String Left = "Left";
    public static String Right = "Right";
    private Boolean saltarPagina = false;
    private Integer banda = 1;
    private String consulta = null;
    private Integer w = 800;
    public Collection<TextField> cTextField = new ArrayList<TextField>();
    public Collection<Field> cField = new ArrayList<Field>();
    public Collection<StaticField> cStaticField = new ArrayList<StaticField>();
    public Collection<StaticField> cStaticFieldNumCol = new ArrayList<StaticField>();
    private TextField tFieldUltimo = null;
    private String saltoDePagina = null;
    private Integer idice = 0;

    /** Creates a new instance of JasperRerportXML */
    public JasperRerportXML() {
    }

    public void setBanda(Integer banda) {
        this.banda = banda;
    }

    public void addTextField(String text, String classs, String formato, Integer sizeText, String justificacion, Integer w, Integer h, Integer x, Integer y, boolean border, boolean negrilla, String TomarDatoDe, boolean estiras) {
        TextField tField = new TextField();
        tField.setEstirar(estiras);
        tField.setBanda(this.banda);
        tField.setTomarDatoDe(TomarDatoDe);
        tField.setClasss(classs);
        tField.setFormato(formato);
        tField.setIdice(idice++);
        tField.setSizeText(sizeText);
        tField.setJustificacion(justificacion);
        tField.setW(w);
        tField.setH(h);
        tField.setX(x);
        tField.setY(y);
        tField.setBorder(border);
        tField.setNegrilla(negrilla);
        tField.setText(text);
        cTextField.add(tField);
        tFieldUltimo = tField;
        
        try {
            text = text.trim();
            if (text.contains("_")) {
                String[] vText = text.trim().split("_");
                String NUMER = vText[vText.length - 1];
                Integer numCol = Integer.valueOf(NUMER);
                int _i=tField.getIdice();
                int _w=tField.getW();
                StaticField _tField = new StaticField();
                _tField.setBanda(this.banda);
                _tField.setIdice(_i);
                _tField.setSizeText(sizeText);
                _tField.setJustificacion(JasperRerportXML.Center);
                _tField.setW(w);
                _tField.setH(h);
                _tField.setX(x);
                _tField.setReSisseText(true);
                _tField.setY(y);
                _tField.setText(numCol.toString());
                _tField.setW(_w);
                _tField.setContar(true);
                _tField.setBorde(false);
                cStaticFieldNumCol.add(_tField);
            }
        } catch (Exception e) { 
        }         
    }

    public void addTextField(String text, String classs, String formato, Integer sizeText, String justificacion, Integer w, Integer h, Integer x, Integer y, boolean border, boolean negrilla, String TomarDatoDe, String valor,boolean estiras) {
        TextField tField = new TextField();
        tField.setEstirar(estiras);
        tField.setBanda(this.banda);
        tField.setTomarDatoDe(TomarDatoDe);
        tField.setClasss(classs);
        tField.setFormato(formato);
        tField.setIdice(idice++);
        tField.setSizeText(sizeText);
        tField.setJustificacion(justificacion);
        tField.setW(w);
        tField.setH(h);
        tField.setX(x);
        tField.setY(y);
        tField.setBorder(border);
        tField.setNegrilla(negrilla);
        tField.setTextFijo(text);
        tField.setValor(valor);
        cTextField.add(tField);
        tFieldUltimo = tField;
        
       
    }

    public void addField(String text, String classs) {
        Field tField = new Field();
        tField.setBanda(this.banda);
        tField.setClasss(classs);
        tField.setIdice(idice++);
        tField.setText(text);
        cField.add(tField);
    }

    public void addStatic(String text, Integer sizeText, String justificacion, Integer w, Integer h, Integer x, Integer y, boolean contar, boolean reSizeText, boolean borde) {
        StaticField tField = new StaticField();
        tField.setBanda(this.banda);
        tField.setIdice(idice++);
        tField.setSizeText(sizeText);
        tField.setJustificacion(justificacion);
        tField.setW(w);
        tField.setH(h);
        tField.setX(x);
        tField.setReSisseText(reSizeText);
        tField.setY(y);
        tField.setText(text);
        tField.setContar(contar);
        tField.setBorde(borde);
        cStaticField.add(tField);


    }
    private String linea = null;

    public void addLine(Integer x) {
        StringBuilder JRXML = new StringBuilder("");

        JRXML.append("<line direction=\"TopDown\">\n");
        JRXML.append("<reportElement\n");
        JRXML.append("x=\"" + (x - 1) + "\"\n");
        JRXML.append("y=\"2\"\n");
        JRXML.append("width=\"1\"\n");
        JRXML.append("height=\"15\"\n");
        JRXML.append("key=\"line-1\"/>\n");
        JRXML.append("<graphicElement stretchType=\"NoStretch\"/>\n");
        JRXML.append("</line>\n");

        JRXML.append("<line direction=\"TopDown\">\n");
        JRXML.append("<reportElement\n");
        JRXML.append("x=\"" + (x - 1) + "\"\n");
        JRXML.append("y=\"20\"\n");
        JRXML.append("width=\"1\"\n");
        JRXML.append("height=\"30\"\n");
        JRXML.append("key=\"line-1\"/>\n");
        JRXML.append("<graphicElement stretchType=\"NoStretch\"/>\n");
        JRXML.append("</line>\n");
        linea = JRXML.toString();
    }

    public void setConsulta(String consulta) {
        this.consulta = consulta;
    }

    public void setSaltarPagina(Boolean _saltarPagina) {
        this.saltarPagina = _saltarPagina != null ? _saltarPagina : false;
    }

    public String toString() {

        StringBuilder JRXML = new StringBuilder("");
        JRXML.append("<?xml version=\"1.0\" encoding=\"UTF-8\"  ?>\n");
        JRXML.append("<!DOCTYPE jasperReport PUBLIC \"//JasperReports//DTD Report Design//EN\" \"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd\">\n");
        JRXML.append("<jasperReport\n");
        JRXML.append(" name=\"BuildIGFormato\"\n");
        JRXML.append(" columnCount=\"1\"\n");
        JRXML.append(" printOrder=\"Vertical\"\n");
        JRXML.append(" orientation=\"Landscape\"\n");
        JRXML.append(" pageWidth=\"" + getW() + "\"\n");
        JRXML.append(" pageHeight=\"" + (getW() <= 612 ? 792 : 612) + "\"\n");
        JRXML.append(" columnWidth=\"" + (getW() - 60) + "\"\n");
        JRXML.append(" columnSpacing=\"0\"\n");
        JRXML.append(" leftMargin=\"30\"\n");
        JRXML.append(" rightMargin=\"30\"\n");
        JRXML.append(" topMargin=\"20\"\n");
        JRXML.append(" bottomMargin=\"20\"\n");
        JRXML.append(" whenNoDataType=\"NoPages\"\n");
        JRXML.append(" isTitleNewPage=\"false\"\n");
        JRXML.append(" isSummaryNewPage=\"false\">\n");

        JRXML.append(" <property name=\"ireport.scriptlethandling\" value=\"0\" />\n");
        JRXML.append(" <property name=\"ireport.encoding\" value=\"UTF-8\"  />\n");
        JRXML.append(" <import value=\"java.util.*\" />\n");
        JRXML.append(" <import value=\"net.sf.jasperreports.engine.*\" />\n");
        JRXML.append(" <import value=\"net.sf.jasperreports.engine.data.*\" />\n");

        JRXML.append(" <parameter name=\"ENTIDAD\" isForPrompting=\"false\" class=\"java.lang.String\">\n");
        JRXML.append("                  <defaultValueExpression ><![CDATA[\"IG\"]]></defaultValueExpression>\n");
        JRXML.append(" </parameter>\n");

        JRXML.append(" <parameter name=\"TIPO_ENTIDAD\" isForPrompting=\"false\" class=\"java.lang.String\">\n");
        JRXML.append("                  <defaultValueExpression ><![CDATA[\"IG\"]]></defaultValueExpression>\n");
        JRXML.append(" </parameter>\n");

        JRXML.append(" <parameter name=\"COD_ENTIDAD\" isForPrompting=\"false\" class=\"java.lang.String\">\n");
        JRXML.append("                  <defaultValueExpression ><![CDATA[\"IG\"]]></defaultValueExpression>\n");
        JRXML.append(" </parameter>\n");

        JRXML.append(" <parameter name=\"SUBCUENTA\" isForPrompting=\"false\" class=\"java.util.Hashtable\">\n");
        JRXML.append("                  <defaultValueExpression ><![CDATA[new java.util.Hashtable()]]></defaultValueExpression>\n");
        JRXML.append(" </parameter>\n");
        JRXML.append(" <parameter name=\"FORMATO\" isForPrompting=\"false\" class=\"java.lang.String\">\n");
        JRXML.append(" 			<defaultValueExpression ><![CDATA[\".\"]]></defaultValueExpression>\n");
        JRXML.append(" </parameter>\n");
        JRXML.append(" <parameter name=\"FECHA_CORTE\" isForPrompting=\"false\" class=\"java.sql.Timestamp\"/>\n");
        JRXML.append(" <queryString><![CDATA[" + this.consulta + "]]></queryString>\n");
        //JRXML.append(" <queryString language=\"xPath\"><![CDATA[" + this.consulta + "]]></queryString>\n");

        JRXML.append(" <!--FIELD-->\n");

        for (Field elem : cField) {
            JRXML.append(elem.toString() + "\n");
        }

        JRXML.append(" <!--FIELD-->\n");
        String SALTO = saltarPagina ? " isStartNewPage=\"true\" " : " ";
        if (saltoDePagina != null) {
            JRXML.append(" 		<group  name=\"" + saltoDePagina + "\" " + SALTO + " >\n");
            JRXML.append(" 		<groupExpression><![CDATA[$F{" + saltoDePagina + "}]]></groupExpression>\n");
            JRXML.append(" 		<groupHeader>\n");
            JRXML.append(" 		<band height=\"10\"  isSplitAllowed=\"true\" >\n");
            JRXML.append("			<textField isStretchWithOverflow=\"false\" isBlankWhenNull=\"false\" evaluationTime=\"Now\" hyperlinkType=\"None\"  hyperlinkTarget=\"Self\" >");
            JRXML.append("				<reportElement");
            JRXML.append("					mode=\"Opaque\"");
            JRXML.append("					x=\"0\"");
            JRXML.append("					y=\"2\"");
            JRXML.append("					width=\"" + (getW() - 60) + "\"");
            JRXML.append("					height=\"8\"");
            JRXML.append("					backcolor=\"#CCCCCC\"");
            JRXML.append("					key=\"textField-8\"/>");
            JRXML.append("				<box topBorder=\"Thin\" topBorderColor=\"#000000\" leftBorder=\"Thin\" leftBorderColor=\"#000000\" rightBorder=\"Thin\" rightBorderColor=\"#000000\" bottomBorder=\"Thin\" bottomBorderColor=\"#000000\"/>");
            JRXML.append("				<textElement textAlignment=\"Left\" verticalAlignment=\"Middle\">");
            JRXML.append("					<font pdfFontName=\"Helvetica-Bold\" size=\"8\" isBold=\"true\"/>");
            JRXML.append("				</textElement>");
            JRXML.append("			<textFieldExpression   class=\"java.lang.String\"><![CDATA[$F{" + saltoDePagina + "}.toString()]]></textFieldExpression>");
            JRXML.append("			</textField>");
            JRXML.append(" 		</band>\n");
            JRXML.append(" 		</groupHeader>\n");
            JRXML.append(" 		<groupFooter>\n");
            JRXML.append(" 		<band height=\"0\"  isSplitAllowed=\"true\" >\n");
            JRXML.append(" 		</band>\n");
            JRXML.append(" 		</groupFooter>\n");
            JRXML.append(" 	</group>\n");
        }

        JRXML.append(" <background>\n");
        JRXML.append("      <band height=\"0\"  isSplitAllowed=\"true\" >\n");
        JRXML.append("      </band>\n");
        JRXML.append(" 	</background>\n");
        JRXML.append("	<title>\n");
        JRXML.append("		<band height=\"48\"  isSplitAllowed=\"true\" >\n");

        JRXML.append(" <!--TEXTFIELD-->\n");

        for (TextField elem : this.cTextField) {
            if (elem.getBanda().equals(1)) {
                JRXML.append(elem.toString() + "\n");
            }
        }

        JRXML.append(" <!--TEXTFIELD-->\n");

        JRXML.append(" </band>\n");
        JRXML.append("</title>\n");

        JRXML.append("	<pageHeader>\n");
        JRXML.append("		<band height=\"41\"  isSplitAllowed=\"true\" >\n");
        JRXML.append(" <!--STATICTEXT-->\n");

        for (StaticField elem : this.cStaticField) {
            JRXML.append(elem.toString() + "\n");
        }
        if (this.linea != null) {
            JRXML.append(this.linea);
        }
        JRXML.append(" <!--STATICTEXT-->\n");
        JRXML.append(" </band>\n");
        JRXML.append("</pageHeader>\n");


        JRXML.append("<columnHeader>\n");
        JRXML.append("  <band height=\"0\"  isSplitAllowed=\"true\" >\n");
        JRXML.append("  </band>\n");
        JRXML.append(" </columnHeader>\n");

        JRXML.append(" <detail>\n");
        JRXML.append("      <band height=\"14\"  isSplitAllowed=\"true\" >\n");
        JRXML.append(" <!--TEXTFIELD-->\n");

        for (TextField elem : this.cTextField) {
            if (elem.getBanda().equals(3)) {
                if (elem.getText().equals(this.tFieldUltimo.getText())) {
                    elem.setFullBorder(true);
                }
                if (elem.getText().equals("UNIDAD_DE_CAPTURA")) {
                    elem.setClasss("java.lang.String");
                    elem.setFormato(null);
                    Object d = Double.valueOf("1.0");
                    elem.setJustificacion(JasperRerportXML.Center);
                    elem.setValor("(new Integer(Double.valueOf($F{UNIDAD_DE_CAPTURA}.toString()).intValue()).toString().length()<2?\"0\"+new Integer(Double.valueOf($F{UNIDAD_DE_CAPTURA}.toString()).intValue()).toString():new Integer(Double.valueOf($F{UNIDAD_DE_CAPTURA}.toString()).intValue()).toString())");
                }
                JRXML.append(elem.toString() + "\n");
            }
        }

        JRXML.append(" <!--TEXTFIELD-->\n");
        JRXML.append("      </band>\n");
        JRXML.append("</detail>\n");
        JRXML.append("	<columnFooter>\n");
        JRXML.append("		<band height=\"0\"  isSplitAllowed=\"true\" >\n");
        JRXML.append("		</band>\n");
        JRXML.append("	</columnFooter>\n");
        JRXML.append("	<pageFooter>\n");
        JRXML.append("		<band height=\"15\"  isSplitAllowed=\"true\" >\n");
        for (StaticField elem : this.cStaticFieldNumCol) {
            elem.setY(1);
            elem.setH(14);
            JRXML.append(elem.toString() + "\n");
        }
        /*JRXML.append("			<textField isStretchWithOverflow=\"false\" pattern=\"EEEEE dd MMMMM yyyy\" isBlankWhenNull=\"false\" evaluationTime=\"Now\" hyperlinkType=\"None\"  hyperlinkTarget=\"Self\" >\n");
        JRXML.append("				<reportElement\n");
        JRXML.append("					x=\"48\"\n");
        JRXML.append("					y=\"2\"\n");
        JRXML.append("					width=\"202\"\n");
        JRXML.append("					height=\"8\"\n");
        JRXML.append("					forecolor=\"#000000\"\n");
        JRXML.append("					key=\"textField-6\"/>\n");
        JRXML.append("				<box topBorder=\"None\" topBorderColor=\"#000000\" leftBorder=\"None\" leftBorderColor=\"#000000\" rightBorder=\"None\" rightBorderColor=\"#000000\" bottomBorder=\"1Point\" bottomBorderColor=\"#000000\"/>\n");
        JRXML.append("				<textElement textAlignment=\"Left\" verticalAlignment=\"Middle\">\n");
        JRXML.append("					<font pdfFontName=\"Helvetica-Oblique\" size=\"5\" isBold=\"false\" isItalic=\"true\" isUnderline=\"false\"/>\n");
        JRXML.append("				</textElement>\n");
        JRXML.append("			<textFieldExpression   class=\"java.util.Date\"><![CDATA[java.util.Calendar.getInstance().getTime()]]></textFieldExpression>\n");
        JRXML.append("			</textField>\n");
        JRXML.append("			<textField isStretchWithOverflow=\"false\" pattern=\"\" isBlankWhenNull=\"false\" evaluationTime=\"Now\" hyperlinkType=\"None\"  hyperlinkTarget=\"Self\" >\n");
        JRXML.append("				<reportElement\n");
        JRXML.append("					x=\"0\"\n");
        JRXML.append("					y=\"2\"\n");
        JRXML.append("					width=\"49\"\n");
        JRXML.append("					height=\"8\"\n");
        JRXML.append("					key=\"textField-7\"/>\n");
        JRXML.append("				<box topBorder=\"None\" topBorderColor=\"#000000\" leftBorder=\"None\" leftBorderColor=\"#000000\" rightBorder=\"None\" rightBorderColor=\"#000000\" bottomBorder=\"1Point\" bottomBorderColor=\"#000000\"/>\n");
        JRXML.append("				<textElement>\n");
        JRXML.append("					<font size=\"5\"/>\n");
        JRXML.append("				</textElement>\n");
        JRXML.append("			<textFieldExpression   class=\"java.lang.Integer\"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>\n");
        JRXML.append("			</textField>\n");*/
        JRXML.append("		</band>\n");
        JRXML.append("	</pageFooter>\n");
        JRXML.append("	<summary>\n");
        JRXML.append("		<band height=\"0\"  isSplitAllowed=\"true\" >\n");
        JRXML.append("		</band>\n");
        JRXML.append("	</summary>\n");

        ///finaliza
        JRXML.append("</jasperReport>");
        return JRXML.toString();
    }

//    public static void main(String[] arg){
//        try{
//            JasperRerportXML jr=new JasperRerportXML();
//
//            jr.setBanda(1);
//            jr.addTextField("ENTIDAD","java.lang.String",null,6,jr.Left,300,12,0,0,false,true,jr.TOMAR_DATO_DE_FIELD,true);
//            jr.addTextField("FORMATO","java.lang.String",null,6,jr.Center,300,12,0,12,false,true,jr.TOMAR_DATO_DE_FIELD,true);
//            jr.addTextField("FECHA_CORTE","java.sql.Timestamp",null,6,jr.Center,300,12,0,24,false,true,jr.TOMAR_DATO_DE_FIELD,true);
//
//
//            java.util.Collection<String> c=new java.util.ArrayList<String>();
//            c.add("UNIDAD DE CAPTURA");
//            c.add("ENTAsdfsdfsdfsdfDAD");
//            c.add("ENTIDAD");
//            c.add("ENTI");
//            c.add("ENTI");
//
//            jr.setBanda(2);
//            for(String elem: c){
//                Integer x=0;
//                Integer y=0;
//                for(StaticField elem1: jr.cStaticField){
//                    x+=elem1.getW();
//                }
//                jr.addStatic(elem,4,jr.Center,0,21,x,2,true,true,true);
//            }
//
//
//            c=new java.util.ArrayList<String>();
//            c.add("UNIDAD DE CAPTURA");
//            c.add("jsanchez");
//            c.add("ENTIDAD");
//            c.add("EN-TI");
//            c.add("EN-TI");
//            jr.setBanda(3);
//
//            for(String elem: c){
//                Integer x=0;
//                for(TextField elem1: jr.cTextField){
//                    if(elem1.getBanda().equals(3))
//                        x+=elem1.getW();
//                }
//                jr.addTextField(elem,"java.lang.String",null,4,jr.Center,0,14,x,0,true,false,jr.TOMAR_DATO_DE_FIELD,true);
//            }
//
//
//            System.out.println("----------");
//            System.out.println(jr);
//            System.out.println("----------");
//            FileWriter write=new FileWriter(new File("reporte/tmp/Build.jrxml"));
//            write.write(jr.toString());
//            write.flush();
//            write.close();
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
//    }
    public Integer getW() {
        return w;
    }

    public void setW(Integer w) {
        this.w = w;
    }

    public Integer getIdice() {
        return idice;
    }

    public void setIdice(Integer idice) {
        this.idice = idice;
    }

    public String getSaltoDePagina() {
        return saltoDePagina;
    }

    public void setSaltoDePagina(String saltoDePagina) {
        this.saltoDePagina = saltoDePagina;
    }
}


